﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UniMana.Utils.Controls;
using UniMana.Utils.Configs;

namespace UniMana.Data
{
    public class Training_Student_ClassInfo
    {
        #region /*Không thay đổi*/
        public bds m_bds = new bds();
        public void Save()
        {
            m_bds.EndEdit();
            m_data.m_DCSave.SubmitChanges();
        }
        public void Delete()
        {
            m_bds.DeleteCurrent();
        }
        #endregion /*Không thay đổi*/
        private DBMain m_data = new DBMain();

        #region [Properties - Thuộc tính]
        public Training_Student_Class Properties
        {
            get { return m_bds.Current as Training_Student_Class; }
        }
        #endregion
        public Training_Student_ClassInfo()
        {
            m_bds.DataSource = typeof(Training_Student_Class);
        }

        public Training_Student_ClassInfo(string nguyenhuu, string studentKey = DefaultConfig.EMPTY_GUID, int majorKey = 0)
        {
            var IQuery = m_data.m_DCSave.Training_Student_Classes.Where(t => t.Training_StudentKey == Guid.Parse(studentKey) &&
                                                                    t.Catalogue_MajorKey == majorKey);
            m_bds.DataSource = IQuery;

            var Row = IQuery.FirstOrDefault();

            if (Row == null)
            {
                Training_Student_Class _new = new Training_Student_Class();
                //_new.Training_StudentKey = Guid.Parse(studentKey);
                //_new.Training_Subject_ClassKey = subjectClassKey;
                Row = _new;
                m_bds.Add(Row);
            }
        }


        public Training_Student_ClassInfo(string studentID = "")
        {
            var IQuery = m_data.m_DCSave.Training_Student_Classes.Where(t => t.ID == studentID);
            m_bds.DataSource = IQuery;
            var Row = IQuery.FirstOrDefault();
            if (Row == null)
            {
                Training_Student_Class _new = new Training_Student_Class();
                //_new.Training_StudentKey = Guid.Parse(studentKey);
                //_new.Training_Subject_ClassKey = subjectClassKey;
                Row = _new;
                m_bds.Add(Row);
            }
        }
        public Training_Student_ClassInfo(Guid studentKey)
        {
            var IQuery = m_data.m_DCSave.Training_Student_Classes.Where(t => t.Training_StudentKey == studentKey);
            m_bds.DataSource = IQuery;
            var Row = IQuery.FirstOrDefault();
            if (Row == null)
            {
                Training_Student_Class _new = new Training_Student_Class();
                Row = _new;
                m_bds.Add(Row);
            }
        }
        #region [An]
        /// <summary>
        /// Lấy thông tin với studentkey và mã lớp
        /// </summary>
        /// <param name="DefaultKey"></param>
        /// <param name="m_iClassKey"></param>

        public Training_Student_ClassInfo(string Training_StudentKey, int m_iTraining_ClassKey, string tuanan = "nguyen.tuanan")
        {
            var IQuery = m_data.m_DCSave.Training_Student_Classes.Where(tb => tb.Training_StudentKey.ToString() == Training_StudentKey && tb.Training_ClassKey == m_iTraining_ClassKey);
            m_bds.DataSource = IQuery;
            var Row = IQuery.FirstOrDefault();
            if (Row == null)
            {
                Row = new Training_Student_Class();
                m_bds.Add(Row);
            }
        }

        /// <summary>
        /// Lấy thông tin với studentKey, bậc, ngành, loại hình, khóa học
        /// </summary>
        /// <param name="m_strStudentKey"></param>
        /// <param name="m_iLevel"></param>
        /// <param name="m_iMajor"></param>
        /// <param name="m_iTrainingType"></param>
        /// <param name="m_iCourse"></param>

        public Training_Student_ClassInfo(string tuanan,
            string m_strStudentKey = DefaultConfig.EMPTY_GUID,
            int m_iLevel = 0,
            int m_iMajor = 0,
            int m_iTrainingType = 0,
            int m_iCourse = 0)
        {
            var IQuery = m_data.m_DCSave.Training_Student_Classes.Where(tb => tb.Training_StudentKey.ToString() == m_strStudentKey && tb.Catalogue_LevelKey == m_iLevel & tb.Catalogue_MajorKey == m_iMajor & tb.Catalogue_TrainingTypeKey == m_iTrainingType & tb.Catalogue_CourseKey == m_iCourse & tb.Mainly == true);
            m_bds.DataSource = IQuery;
            var Row = IQuery.FirstOrDefault();
            if (Row == null)
            {
                Row = new Training_Student_Class();
                m_bds.Add(Row);
            }
        }

        /// <summary>
        /// Lấy thông tin với studentKey, bậc, ngành, loại hình, khóa học, lớp
        /// </summary>
        /// <param name="m_strStudentKey"></param>
        /// <param name="m_iLevel"></param>
        /// <param name="m_iMajor"></param>
        /// <param name="m_iTrainingType"></param>
        /// <param name="m_iCourse"></param>

        public Training_Student_ClassInfo(string m_strStudentKey,
            int m_iLevel,
            int m_iMajor,
            int m_iTrainingType,
            int m_iCourse,
            int m_iTrainingClassKey)
        {
            var IQuery = m_data.m_DCSave.Training_Student_Classes.Where(tb => tb.Training_StudentKey.ToString() == m_strStudentKey && tb.Catalogue_LevelKey == m_iLevel & tb.Catalogue_MajorKey == m_iMajor & tb.Catalogue_TrainingTypeKey == m_iTrainingType & tb.Catalogue_CourseKey == m_iCourse & tb.Training_ClassKey == m_iTrainingClassKey);
            m_bds.DataSource = IQuery;
            var Row = IQuery.FirstOrDefault();
            if (Row == null)
            {
                Row = new Training_Student_Class();
                m_bds.Add(Row);
            }
        }

        /// <summary>
        /// Chọn sinh viên chuyển ngành, chuyển khóa
        /// </summary>
        /// <param name="m_strStudentKey"></param>
        /// <param name="m_iTraining_ProgramKey"></param>

        public Training_Student_ClassInfo(string m_strStudentKey, int m_iTraining_ProgramKey, bool mainly)
        {
            var IQuery = m_data.m_DCSave.Training_Student_Classes.Where(tb => tb.Training_StudentKey.ToString() == m_strStudentKey && tb.Training_TrainingProgramKey == m_iTraining_ProgramKey && tb.Mainly == mainly);
            m_bds.DataSource = IQuery;
            var Row = IQuery.FirstOrDefault();
            if (Row == null)
            {
                Row = new Training_Student_Class();
                m_bds.Add(Row);
            }
        }

        /// <summary>
        /// Tạo mới đối tượng Training_Student_Class
        /// Nếu Training_StudentKey = rỗng và MajorKey =0 thì m_bds trỏ tới đối tượng mới
        /// Ngược lại m_bds trỏ tới đối tượng có Training_StudentKey và MajorKey truyền vào
        /// </summary>
        /// <param name="nTrainingStudentKey">Training_StudentKey</param>
        /// <param name="nMajorKey">Catalogue_MajorKey</param>
        public Training_Student_ClassInfo(string tuanan = "nguyen.tuanan", string nTrainingStudentKey = DefaultConfig.EMPTY_GUID, int nMajorKey = 0, bool bMainly = true)
        {
            var Iquery = m_data.m_DCSave.Training_Student_Classes.Where(p => p.Training_StudentKey == Guid.Parse(nTrainingStudentKey)
                                                                           && p.Catalogue_MajorKey == nMajorKey
                                                                           && p.Mainly == true);
            m_bds.DataSource = Iquery;
            var Row = Iquery.FirstOrDefault();
            if (Row == null)
            {
                Row = new Training_Student_Class();
                Row.Training_StudentKey = Guid.Parse(nTrainingStudentKey);
                Row.Catalogue_MajorKey = nMajorKey;
                Row.Mainly = bMainly;
                m_bds.Add(Row);
                m_bds.MoveLast();
            }
        }

        ///////// <summary>
        ///////// Tìm sinh viên với mã và CTĐT
        ///////// </summary>
        ///////// <param name="m_strStudentKey"></param>
        ///////// <param name="m_iTraining_ProgramKey"></param>

        //////public Training_Student_ClassInfo(string m_strStudentKey, int m_iTraining_ProgramKey, string text = "" )
        //////{
        //////    var IQuery = m_data.m_DCSave.Training_Student_Classes.Where(tb => tb.Training_StudentKey.ToString() == m_strStudentKey && tb.Training_TrainingProgramKey == m_iTraining_ProgramKey);
        //////    m_bds.DataSource = IQuery;
        //////    var Row = IQuery.FirstOrDefault();
        //////    if (Row == null)
        //////    {
        //////        Row = new Training_Student_Class();
        //////        m_bds.Add(Row);
        //////    }
        //////}

        /// <summary>
        /// Lấy thông tin sinh viên theo Key và ID
        /// </summary>
        /// <param name="m_strStudentKey"></param>
        /// <param name="m_strID"></param>

        public Training_Student_ClassInfo(string m_strStudentKey, string m_strID)
        {
            var IQuery = m_data.m_DCSave.Training_Student_Classes.Where(tb => tb.Training_StudentKey.ToString() == m_strStudentKey && tb.ID == m_strID && tb.Mainly == true);
            m_bds.DataSource = IQuery;
            var Row = IQuery.FirstOrDefault();
            if (Row == null)
            {
                Row = new Training_Student_Class();
                m_bds.Add(Row);
            }
        }
        #endregion
    }
}
